'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {DatePicker, Form, Select, Tabs} from 'antd';
import styles from '../index.less';
import ViewTitle from '../../components/ViewTitle';
import FormBase from '../../components/Base/FormBase';
import {FormField, SearchForm} from 'antd-extra';
import moment from 'moment';
import 'moment/locale/zh-cn';
import PaymentAssess from "./assess/PaymentAssess";
import ReceiptAssess from "./assess/ReceiptAssess";
import ReceiptTax from "./assess/ReceiptTax";
import PaymentTax from "./assess/PaymentTax";
import StoreAssess from "./assess/StoreAssess";
import MoneyAssess from "./assess/MoneyAssess";
import FixedAssets from "./assess/FixedAssets";

moment.locale('zh-cn');
const {RangePicker} = DatePicker;
const _ = require('lodash');

const {Option} = Select;
@Form.create()
@connect(({
            user,
            global,
            store,
            dict,
            finance
          }) => ({...user, ...global, ...dict, ...finance, ...store}))
export default class Assess extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table: 'Payment'}
    });
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table: 'Receipt'}
    });
    const payload = this.props.form.getFieldsValue();
    _.map(payload, (value, key) => {
      if (value === '') {
        payload[key] = undefined;
      }
    });
    const {accountCode = ""} = this.state;
    payload.accountCode = accountCode;
    _.map(payload, (value, key) => {
      if (value === '') {
        payload[key] = undefined;
      }
    });
    const _payload = this.state.accountCode ? {code: this.state.accountCode} : {};
    console.log('_pay', _payload);
    console.log('payloadpayload', payload);
    this.props.dispatch({
      type: 'finance/findAccounts',
      payload: _payload
    });
    this.props.dispatch({
      type: 'finance/assess',
      payload
    });
    this.props.dispatch({
      type: 'finance/taxAssess',
      payload
    });
    this.props.dispatch({
      type: 'finance/fixedAssess',
      payload
    });
  }


  render() {
    const {accounts = [],  form} = this.props;

    return (
      <Form className={styles.content}>
        <ViewTitle
          title={`资产统计`}
          level={1}
        ></ViewTitle>
        <SearchForm
          style={{marginLeft: '2px', marginRight: '2px'}}
          onSearch={() => {
            this.state.current = 1;
            this.componentDidMount();
          }}
          onReset={this.handleReset}
          cols={3}
          min={3}
          expandText="展开"
          form={form}
        >
          {[
            <FormField
              label={'账户'}
              name={'account'}
              key={'account'}
            >
              <Select allowClear placeholder="请选择" style={{width: '170%'}}
                      onChange={(value, option) => {
                        const {key} = option;
                        this.state.accountCode = key;
                      }}
              >
                {this.getAccount(accounts)}
              </Select>
            </FormField>,

            <FormField
              label={"统计时间"}
              name={"createdTs"}
              key={"createdTs"}
              initialValue={[this.getSearchStart1Months(), this.getDefaultSearchEndDate()]}
            >
              <RangePicker allowClear={true}
                // style={{ width: '150%' }}
              />
            </FormField>
          ]}
        </SearchForm>

        <Tabs defaultActiveKey="0">
          {/*<Tabs.TabPane tab="固定资产" key="0">*/}
          {/*  <FixedAssets></FixedAssets>*/}
          {/*</Tabs.TabPane>*/}
          <Tabs.TabPane tab="付款统计" key="1">
            <PaymentAssess></PaymentAssess>
          </Tabs.TabPane>
          <Tabs.TabPane tab="收款统计" key="2">
            <ReceiptAssess></ReceiptAssess>
          </Tabs.TabPane>
          <Tabs.TabPane tab="应收税额" key="3">
            <ReceiptTax></ReceiptTax>
          </Tabs.TabPane>
          <Tabs.TabPane tab="应付税额" key="4">
            <PaymentTax></PaymentTax>
          </Tabs.TabPane>
          <Tabs.TabPane tab="资金统计" key="6">
            <MoneyAssess></MoneyAssess>
          </Tabs.TabPane>
          <Tabs.TabPane tab="固定资产" key="7">
            <FixedAssets></FixedAssets>
          </Tabs.TabPane>
          <Tabs.TabPane tab="库存统计" key="5">
            <StoreAssess></StoreAssess>
          </Tabs.TabPane>
        </Tabs>

        {/*{showDetail && <Drawer*/}
        {/*  visible={showDetail}*/}
        {/*  title={`统计`}*/}
        {/*  placement="left"*/}
        {/*  width={'80%'}*/}
        {/*  style={{padding: 0}}*/}
        {/*  // maskClosable={false}*/}
        {/*  onClose={() => {*/}
        {/*    this.setState({showDetail: false});*/}
        {/*    this.componentDidMount();*/}
        {/*  }}*/}
        {/*>*/}
        {/*  <ModalShippingFeeDetail*/}
        {/*    values={record}*/}
        {/*  > </ModalShippingFeeDetail>*/}
        {/*</Drawer>}*/}
      </Form>
    );
  }
}
